package Teacher.Penalty;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JTextField;

import Database.CallDB;
import Database.MyTableModel;
import Database.PenaltyMgmt;
import Exception.CheckDataException;
import Exception.EraseDataException;
import Exception.FormatScrollPaneException;
import Exception.InsertionDataException;
import Exception.Layer8DataException;
import Exception.LoadDriverException;
import General.CreaModPanel;
import General.GeneralInformation;
import General.GraphicsSetting;
import General.TreatError;

public class PanelCreateModPenalty extends CreaModPanel
{
	private ArrayList<Integer> arrayPercent, arrayTime;
	private JTextField fieldPercentage, fieldTime;
	private JLabel labelName, labelPercentage, labelTime;
	private boolean modifyFlag = false;
	private int selectedRow;
	
	public PanelCreateModPenalty(CallDB callDB, Connection connection) throws CheckDataException, FormatScrollPaneException
	{
		super(callDB, connection);
		
		//Disable heritage component
		labelDate.setVisible(false);
		date.setVisible(false);
		buttonSummary.setVisible(false);
		
		modifyFlag = false;
		
		creationComponent();
		showComponent();
	}
	
	public PanelCreateModPenalty(CallDB callDB, Connection connection, String fieldName) throws CheckDataException, FormatScrollPaneException, LoadDriverException, InsertionDataException
	{
		super(callDB, connection);
		
		//Disable heritage component
		labelDate.setVisible(false);
		date.setVisible(false);
		buttonSummary.setVisible(false);
		
		modifyFlag = true;
		creationComponent();
		
		fillValueArrayModelField(fieldName);
		
		showComponent();
	}
	
	public void fillValueArrayModelField(String fieldName) throws FormatScrollPaneException, CheckDataException, LoadDriverException, InsertionDataException
	{
		this.fieldName.setText(fieldName);
		MyTableModel tableTemp = new PenaltyMgmt(connection).transferRealToTemporaryTable(GeneralInformation.getIdSelectedLeftTable());
		
		myRightTableModel = tableTemp;
		refreshSuperRightTable();

		//Fill arrayList
		for(int cpt = 0; cpt < myRightTableModel.getRowCount(); cpt++)
		{
			arrayTime.add((int)myRightTableModel.getValueAt(cpt, 0));
			arrayPercent.add((int)myRightTableModel.getValueAt(cpt, 1));
		}
	}

	public void setPenalty() throws InsertionDataException, CheckDataException, EraseDataException, LoadDriverException
	{		
		if(modifyFlag == false)
			new PenaltyMgmt(connection).createElement(fieldName.getText(), arrayTime, arrayPercent);
		else
		{
			new PenaltyMgmt(connection).modifyElement((int)GeneralInformation.getIdSelectedLeftTable(), fieldName.getText(), arrayTime, arrayPercent);
			myRightTableModel = new PenaltyMgmt(connection).selectAllElementPenaltyTemp();
		}
	}
	
	public void suppressRow() throws FormatScrollPaneException, EraseDataException, LoadDriverException, CheckDataException
	{
		new PenaltyMgmt(connection).deleteRow((int)myRightTableModel.getValueAt(selectedRow, 2)); //Delete the selected element
		
		myRightTableModel = new PenaltyMgmt(connection).selectAllElementPenaltyTemp();
		
		arrayTime.clear();
		arrayPercent.clear();
		
		for(int cpt = 0; cpt < myRightTableModel.getRowCount(); cpt++){
			arrayTime.add((int)myRightTableModel.getValueAt(cpt, 0));
			arrayPercent.add((int)myRightTableModel.getValueAt(cpt, 1));}

		buttonSuppress.setEnabled(false);
	}
	
	public void addValue() throws InsertionDataException, CheckDataException, LoadDriverException, EraseDataException
	{
		int time = Integer.parseInt(fieldTime.getText());
		int percent = Integer.parseInt(fieldPercentage.getText());
		
		arrayTime.add(time);
		arrayPercent.add(percent);
		
		myRightTableModel = new PenaltyMgmt(connection).createTempElement(arrayTime, arrayPercent);
		
		fieldPercentage.setText("");
		fieldTime.setText("");
	}

	private class MyActionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0)
		{
			try
			{
				if(arg0.getSource() == buttonDone)
					if(fieldName.getText().isEmpty() == false)
						setPenalty();
					else
						throw new Layer8DataException();
				else if(arg0.getSource() == buttonAdd)
					if(fieldPercentage.getText().isEmpty() == false && fieldTime.getText().isEmpty() == false)
						addValue();
					else
						throw new Layer8DataException();
				else if(arg0.getSource() == buttonSuppress)
					suppressRow();
				
				refreshSuperRightTable();
			}
			catch(CheckDataException | InsertionDataException | EraseDataException | LoadDriverException | Layer8DataException | FormatScrollPaneException | NumberFormatException error)
			{
				TreatError.showError(error);
			}	
		}
	}
	
	private class MyMouseListener implements MouseListener
	{
		public void mouseClicked(MouseEvent arg0)
		{
			if(tableRight.getSelectedRow() != -1)
			{
				selectedRow = tableRight.convertRowIndexToModel(tableRight.getSelectedRow());
				buttonSuppress.setEnabled(true);
			}
		}
		
		public void mouseEntered(MouseEvent arg0){}
		public void mouseExited(MouseEvent arg0){}
		public void mousePressed(MouseEvent arg0){}
		public void mouseReleased(MouseEvent arg0){}
	}
	
	public void creationComponent() throws CheckDataException
	{	
		MyActionListener actionListener = new MyActionListener();
		MyMouseListener mouseListener = new MyMouseListener();
		
		tableRight.addMouseListener(mouseListener);

		labelName = new JLabel("Name");
		labelPercentage = new JLabel("Percentage");		
		labelTime = new JLabel("Time");
		
		fieldTime = new JTextField();
		fieldTime.setColumns(10);

		fieldPercentage = new JTextField();
		fieldPercentage.setColumns(10);
		
		arrayPercent = new ArrayList<Integer>();
		arrayTime = new ArrayList<Integer>();
		
		buttonAdd.addActionListener(actionListener);
		buttonDone.addActionListener(actionListener);
		buttonSuppress.addActionListener(actionListener);
		
		buttonSuppress.setEnabled(false);
	}
	
	public void showComponent() throws FormatScrollPaneException
	{
		labelTime.setBounds(40, 100, 90, 30);
		labelPercentage.setBounds(40, 143, 90, 30);
		buttonAdd.setBounds(205, 100, 73, 73); buttonSuppress.setBounds(410, 363, 330, 45);
		
		fieldTime.setBounds(115, 100, 90, 30);
		fieldPercentage.setBounds(115, 143, 90, 30);
		
		this.add(labelTime);
		this.add(labelPercentage);
		this.add(fieldTime);
		this.add(fieldPercentage);
		this.add(buttonAdd);
		this.add(buttonSuppress);
		
		GraphicsSetting.setBackground(this, "image/main.png", 0, 0, 800, 600);
	}
	
	public void setModifyFlag(boolean value){modifyFlag = value;}
}